<template>
  <div class="tutorials-container">
    <!-- 顶部导航 -->
    <header class="tutorials-header">
      <div class="header-content">
        <nav class="main-nav">
          <button 
            v-for="menu in mainMenus" 
            :key="menu.id"
            :class="['nav-button', { active: activeMainMenu === menu.id }]"
            @click="selectMainMenu(menu.id)"
          >
            {{ menu.title }}
          </button>
        </nav>
      </div>
    </header>

    <!-- 主要内容区 -->
    <main class="tutorials-main">
      <!-- 二级菜单标签页 -->
      <section class="secondary-nav">
        <div class="tags-container">
          <button 
            v-for="subMenu in currentSubMenus" 
            :key="subMenu.id"
            :class="['tag-button', { active: activeSubMenu === subMenu.id }]"
            @click="selectSubMenu(subMenu.id)"
          >
            {{ subMenu.title }}
          </button>
        </div>
      </section>

      <!-- 教程内容区 -->
      <div class="content-wrapper">
        <!-- 左侧章节导航 -->
        <aside class="chapter-sidebar">
          <h3>章节列表</h3>
          <ul class="chapter-list">
            <li 
              v-for="chapter in currentChapters" 
              :key="chapter.id"
              :class="['chapter-item', { active: activeChapter === chapter.id }]"
              @click="selectChapter(chapter.id)"
            >
              {{ chapter.title }}
            </li>
          </ul>
        </aside>

        <!-- 右侧章节内容 -->
        <article class="chapter-content">
          <div v-if="currentChapterContent" class="content-display">
            <h2>{{ currentChapterContent.title }}</h2>
            <div class="content-body" v-html="currentChapterContent.content"></div>
          </div>
          <div v-else class="empty-state">
            <p>请选择一个章节查看内容</p>
          </div>
        </article>
      </div>
    </main>
  </div>
</template>

<script setup>
import { reactive, ref, computed } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()

// 状态管理
const activeMainMenu = ref('backend')
const activeSubMenu = ref('nginx')
const activeChapter = ref(null)

// 一级菜单数据
const mainMenus = reactive([
  { id: 'backend', title: '后端开发' },
  { id: 'frontend', title: '前端开发' },
  { id: 'database', title: '数据库' },
  { id: 'devops', title: '运维部署' },
  { id: 'tools', title: '开发工具' }
])

// 二级菜单数据
const subMenus = reactive({
  backend: [
    { id: 'nginx', title: 'Nginx使用手册' },
    { id: 'linux', title: 'Linux系统管理' },
    { id: 'docker', title: 'Docker容器化' }
  ],
  frontend: [
    { id: 'javascript', title: 'JavaScript高级编程' },
    { id: 'vue', title: 'Vue.js框架实战' },
    { id: 'react', title: 'React开发指南' }
  ],
  database: [
    { id: 'mysql', title: 'MySQL使用手册' },
    { id: 'redis', title: 'Redis缓存技术' },
    { id: 'mongodb', title: 'MongoDB实战' }
  ],
  devops: [
    { id: 'git', title: 'Git版本控制' },
    { id: 'ci_cd', title: 'CI/CD实践' },
    { id: 'kubernetes', title: 'Kubernetes入门' }
  ],
  tools: [
    { id: 'vscode', title: 'VSCode配置指南' },
    { id: 'postman', title: 'Postman接口测试' },
    { id: 'nodejs', title: 'Node.js开发环境' }
  ]
})

// 教程章节数据（模拟）
const tutorialsData = reactive({
  nginx: [
    { id: 1, title: 'Nginx简介', content: '<h3>Nginx是什么？</h3><p>Nginx是一个高性能的HTTP和反向代理web服务器，同时也提供了IMAP/POP3/SMTP服务。</p><p>Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点（俄文：Рамблер）开发的，第一个公开版本0.1.0发布于2004年10月4日。</p><h3>Nginx的主要特点</h3><ul><li>高并发连接：官方测试能支撑5万并发连接</li><li>内存消耗小：在3万并发连接下，开启10个Nginx进程才消耗150M内存</li><li>配置文件非常简单：风格跟程序一样通俗易懂</li><li>支持Rewrite重写规则</li><li>支持虚拟机host</li><li>内置健康检查功能</li></ul>' },
    { id: 2, title: 'Nginx安装', content: '<h3>Linux系统安装Nginx</h3><p>在Ubuntu/Debian系统上：' },
    { id: 3, title: 'Nginx配置', content: '<h3>Nginx配置文件结构</h3><p>Nginx的主要配置文件通常位于<code>/etc/nginx/nginx.conf</code>。' }
  ],
  mysql: [
    { id: 1, title: 'MySQL简介', content: '<h3>什么是MySQL？</h3><p>MySQL是一个关系型数据库管理系统，由瑞典MySQL AB 公司开发，目前属于 Oracle 旗下产品。</p><p>MySQL 是最流行的关系型数据库管理系统之一，在 WEB 应用方面，MySQL是最好的 RDBMS (Relational Database Management System，关系数据库管理系统) 应用软件之一。</p><h3>MySQL的特点</h3><ul><li>MySQL是开源的，所以你不需要支付额外的费用</li><li>MySQL支持大型的数据库，可以处理拥有上千万条记录的大型数据库</li><li>MySQL使用标准的SQL数据语言形式</li><li>MySQL可以运行于多个系统上，并且支持多种语言</li><li>MySQL对PHP有很好的支持，PHP是目前最流行的Web开发语言</li></ul>' },
    { id: 2, title: 'MySQL安装', content: '<h3>Linux系统安装MySQL</h3><p>在Ubuntu/Debian系统上：</p><pre><code>' },
    { id: 3, title: 'MySQL基本操作', content: '<h3>连接MySQL服务器</h3><pre><code>mysql -u root -p</code></pre><h3>创建数据库</h3>' }
  ],
  javascript: [
    { id: 1, title: 'JavaScript基础', content: '<h3>JavaScript简介</h3><p>JavaScript是一种具有函数优先的轻量级，解释型或即时编译型的编程语言。' },
    { id: 2, title: 'ES6新特性', content: '<h3>什么是ES6？</h3><p>ES6（ECMAScript 2015）是JavaScript语言的新一代标准，于2015年6月正式发布。' }
  ]
})

// 计算属性
const currentSubMenus = computed(() => {
  return subMenus[activeMainMenu.value] || []
})

const currentChapters = computed(() => {
  return tutorialsData[activeSubMenu.value] || []
})

const currentChapterContent = computed(() => {
  if (!activeChapter.value || !tutorialsData[activeSubMenu.value]) {
    return null
  }
  return tutorialsData[activeSubMenu.value].find(ch => ch.id === activeChapter.value) || null
})

// 方法
const selectMainMenu = (menuId) => {
  activeMainMenu.value = menuId
  // 切换主菜单时，重置二级菜单和章节选择
  const firstSubMenu = subMenus[menuId]?.[0]
  activeSubMenu.value = firstSubMenu?.id || null
  activeChapter.value = null
}

const selectSubMenu = (subMenuId) => {
  activeSubMenu.value = subMenuId
  // 切换二级菜单时，重置章节选择
  activeChapter.value = null
}

const selectChapter = (chapterId) => {
  activeChapter.value = chapterId
}
</script>

<style scoped lang="scss">
// 现代化配色方案
$primary: #4f46e5;
$primary-light: #818cf8;
$secondary: #06b6d4;
$dark-bg: #0f172a;
$card-bg: #1e293b;
$card-hover: #2d3748;
$text-primary: #f8fafc;
$text-secondary: #cbd5e1;
$border-color: #334155;
$accent: #f59e0b;
$success: #10b981;
$danger: #ef4444;

// 全局样式
.tutorials-container {
  min-height: 100vh;
  background: linear-gradient(135deg, $dark-bg 0%, #131c31 100%);
  color: $text-primary;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

// 顶部导航样式
.tutorials-header {
  background: rgba(15, 23, 42, 0.85);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid $border-color;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);

  .header-content {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0.75rem 1.5rem;

    .main-nav {
      display: flex;
      justify-content: center;
      flex-wrap: wrap;
      gap: 0.75rem;

      .nav-button {
        padding: 0.75rem 1.25rem;
        background: rgba(30, 41, 59, 0.6);
        border: 1px solid transparent;
        border-radius: 8px;
        color: $text-secondary;
        font-size: 0.95rem;
        font-weight: 500;
        cursor: pointer;
        transition: all 0.25s ease;
        letter-spacing: 0.02em;

        &:hover {
          background: $card-hover;
          border-color: $primary-light;
          color: $text-primary;
          transform: translateY(-1px);
          box-shadow: 0 4px 12px rgba(79, 70, 229, 0.15);
        }

        &.active {
          background: linear-gradient(135deg, rgba(79, 70, 229, 0.15), rgba(6, 182, 212, 0.05));
          border-color: $primary;
          color: $primary-light;
          box-shadow: 0 0 15px rgba(79, 70, 229, 0.2);
          transform: translateY(-1px);
        }
      }
    }
  }
}

// 主要内容区域
.tutorials-main {
  max-width: 1200px;
  margin: 0 auto;
  padding: 1.5rem;

  // 二级菜单标签页样式
  .secondary-nav {
    margin: 0 0 2rem 0;
    padding: 1rem;
    background: rgba(30, 41, 59, 0.4);
    border-radius: 12px;
    border: 1px solid $border-color;
    backdrop-filter: blur(8px);

    .tags-container {
      display: flex;
      flex-wrap: wrap;
      gap: 0.5rem;

      .tag-button {
        padding: 0.5rem 1rem;
        background: $card-bg;
        border: 1px solid transparent;
        border-radius: 6px;
        color: $text-secondary;
        font-size: 0.85rem;
        font-weight: 500;
        cursor: pointer;
        transition: all 0.2s ease;
        white-space: nowrap;

        &:hover {
          background: $card-hover;
          border-color: $secondary;
          color: $text-primary;
          transform: translateY(-1px);
        }

        &.active {
          background: linear-gradient(135deg, rgba(6, 182, 212, 0.15), rgba(79, 70, 229, 0.05));
          border-color: $secondary;
          color: $secondary;
          box-shadow: 0 0 10px rgba(6, 182, 212, 0.2);
        }
      }
    }
  }

  // 内容包装器
  .content-wrapper {
    display: grid;
    grid-template-columns: 280px 1fr;
    gap: 2rem;
    margin-top: 0.5rem;

    // 左侧章节导航
    .chapter-sidebar {
      background: $card-bg;
      border-radius: 12px;
      border: 1px solid $border-color;
      overflow: hidden;
      position: sticky;
      top: 90px;
      max-height: calc(100vh - 120px);
      box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1);

      h3 {
        margin: 0;
        padding: 1.25rem 1.5rem;
        background: rgba(79, 70, 229, 0.1);
        color: $primary-light;
        font-size: 1.1rem;
        font-weight: 600;
        border-bottom: 1px solid $border-color;
      }

      .chapter-list {
        list-style: none;
        margin: 0;
        padding: 0.5rem 0;
        overflow-y: auto;
        max-height: calc(100vh - 200px);

        // 滚动条样式
        &::-webkit-scrollbar {
          width: 6px;
        }

        &::-webkit-scrollbar-track {
          background: rgba(0, 0, 0, 0.1);
        }

        &::-webkit-scrollbar-thumb {
          background: $border-color;
          border-radius: 3px;
        }

        &::-webkit-scrollbar-thumb:hover {
          background: rgba(79, 70, 229, 0.3);
        }

        .chapter-item {
          margin: 0 1rem 0.25rem 1rem;
          border-radius: 8px;
          overflow: hidden;
          transition: all 0.2s ease;

          &:hover {
            transform: translateX(4px);
          }

          &.active {
            transform: translateX(4px);
          }

          // 直接应用到li元素，因为模板中没有chapter-title类
          cursor: pointer;
          padding: 0.75rem 1rem;
          background: $card-bg;
          color: $text-secondary;
          border: 1px solid transparent;
          font-size: 0.95rem;
          font-weight: 500;
          transition: all 0.25s ease;
          position: relative;
          overflow: hidden;

          &:hover {
            background: $card-hover;
            color: $text-primary;
            border-color: rgba(79, 70, 229, 0.3);
          }

          &.active {
            background: linear-gradient(90deg, rgba(79, 70, 229, 0.1), rgba(79, 70, 229, 0.05));
            color: $primary-light;
            border-color: $primary;
            font-weight: 600;
          }

          // 左侧指示器
          &:before {
            content: '';
            position: absolute;
            left: 0;
            top: 0;
            height: 100%;
            width: 3px;
            background: $primary;
            transform: scaleY(0);
            transition: transform 0.25s ease;
          }

          &:hover:before,
          &.active:before {
            transform: scaleY(1);
          }
        }
      }
    }

    // 右侧章节内容
    .chapter-content {
      background: $card-bg;
      border-radius: 12px;
      border: 1px solid $border-color;
      padding: 2rem;
      box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1);

      // 内容显示区域
      .content-display {
        h2 {
          margin-top: 0;
          margin-bottom: 1.5rem;
          color: $primary-light;
          font-size: 1.8rem;
          font-weight: 700;
          line-height: 1.3;
          background: linear-gradient(90deg, $primary-light, $secondary);
          -webkit-background-clip: text;
          -webkit-text-fill-color: transparent;
          background-clip: text;
        }

        .content-body {
          color: $text-secondary;
          line-height: 1.7;

          // 富文本内容样式
          h3 {
            color: $secondary;
            margin-top: 2rem;
            margin-bottom: 1rem;
            font-size: 1.4rem;
            font-weight: 600;
          }

          p {
            margin-bottom: 1.5rem;
            color: $text-secondary;
          }

          ul, ol {
            margin-bottom: 1.5rem;
            padding-left: 1.5rem;
          }

          li {
            margin-bottom: 0.75rem;
          }

          code {
            background: rgba(15, 23, 42, 0.5);
            color: $accent;
            padding: 0.2rem 0.4rem;
            border-radius: 4px;
            font-family: 'Fira Code', 'Consolas', monospace;
            font-size: 0.9em;
          }

          pre {
            background: rgba(15, 23, 42, 0.8);
            border: 1px solid $border-color;
            border-radius: 8px;
            padding: 1.5rem;
            margin: 1.5rem 0;
            overflow-x: auto;

            code {
              background: transparent;
              padding: 0;
              border-radius: 0;
              color: $text-primary;
            }
          }

          a {
            color: $primary-light;
            text-decoration: none;
            transition: color 0.2s ease;

            &:hover {
              color: $secondary;
              text-decoration: underline;
            }
          }

          img {
            max-width: 100%;
            height: auto;
            border-radius: 8px;
            border: 1px solid $border-color;
            margin: 1.5rem 0;
            transition: transform 0.3s ease, box-shadow 0.3s ease;

            &:hover {
              transform: translateY(-2px);
              box-shadow: 0 10px 20px rgba(0, 0, 0, 0.3);
            }
          }
        }
      }

      // 空状态样式
      .empty-state {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 300px;
        color: $text-secondary;
        text-align: center;

        p {
          font-size: 1.1rem;
          opacity: 0.7;
        }
      }
    }
  }
}

// 响应式设计
// 平板设备
@media (max-width: 1024px) {
  .tutorials-main {
    .content-wrapper {
      grid-template-columns: 1fr;
      gap: 1.5rem;

      .chapter-sidebar {
        position: static;
        max-height: none;

        .chapter-list {
          max-height: 300px;
        }
      }
    }
  }
}

// 移动设备
@media (max-width: 768px) {
  .tutorials-header {
    .header-content {
      padding: 0.75rem 1rem;

      .main-nav {
        gap: 0.5rem;

        .nav-button {
          padding: 0.6rem 0.9rem;
          font-size: 0.85rem;
        }
      }
    }
  }

  .tutorials-main {
    padding: 1rem;

    .secondary-nav {
      padding: 0.75rem;

      .tags-container {
        gap: 0.3rem;

        .tag-button {
          padding: 0.4rem 0.75rem;
          font-size: 0.8rem;
        }
      }
    }

    .content-wrapper {
      gap: 1rem;

      .chapter-content {
        padding: 1.5rem;

        .content-display {
          h2 {
            font-size: 1.5rem;
          }

          .content-body {
            h3 {
              font-size: 1.2rem;
            }
          }
        }
      }
    }
  }
}

// 小型手机
@media (max-width: 480px) {
  .tutorials-main {
    .content-wrapper {
      .chapter-content {
        padding: 1.25rem 1rem;
      }
    }
  }
}
</style>